var MsgUer = sessionStorage.getItem("user");
 MsgUer = JSON.parse(MsgUer);
$(function (){
    //调用初始化方法
    initPage(MsgUer);

    //点击确认收货
});
//初始化页面
function initPage(obj){
    $.get(
        "orderQueryByUserid",
        {uid:obj.id},
        function (resp){
            var orders=resp.data;
            for (let i = 0; i < orders.length; i++) {
                var div=$(`<div class="order-item">
                                 <div class="row">
                                  <div class="col-6">
                                   <div class="form-group form-row my-0">
                                    <label class="col-form-label">订单号</label>
                                    <div class="col form-control-plaintext">
                                     <span class="text-primary">${orders[i].id}</span>
                                    </div>
                                   </div>
                                  </div>
                            
                                  <div class="col-6">
                                   <div class="form-group form-row my-0">
                                    <label class="col-form-label">收货人</label>
                                    <div class="col form-control-plaintext">
                                     ${orders[i].username} [<span class="text-primary">${orders[i].tel}</span>]
                                    </div>
                                   </div>
                                  </div>
                                 </div>
                            
                                 <div class="row">
                                  <div class="col-6">
                                   <div class="form-group form-row my-0">
                                    <label class="col-form-label">订单状态</label>
                                    <div class="col form-control-plaintext">
                                     <span class="text-success">
                                        ${orders[i].status==1?"发货中":""}
                                        ${orders[i].status==2?"送货中":""}
                                        ${orders[i].status==3?"已收货":""}
                                        ${orders[i].status==4?"您的快递真正被攻击":""}
                                        ${orders[i].status==5?"商家跑路了":""}
                                     </span>
                                    </div>
                                   </div>
                                  </div>
                            
                                  <div class="col-6">
                                   <div class="form-group form-row my-0">
                                    <label class="col-form-label">订单金额</label>
                                    <div class="col form-control-plaintext">￥${orders[i].totalamount}</div>
                                   </div>
                                  </div>
                                 </div>
                            
                                 <div class="row">
                                  <div class="col-6">
                                   <div class="form-group form-row my-0">
                                    <label class="col-form-label">订单时间</label>
                                    <div class="col form-control-plaintext">${orders[i].creationtime}</div>
                                   </div>
                                  </div>
                            
                                  <div class="col-6">
                                   <div class="form-group form-row my-0">
                                    <label class="col-form-label">收货地址</label>
                                    <div class="col form-control-plaintext">${orders[i].address}</div>
                                   </div>
                                  </div>
                                 </div>
                            
                                 <div class="d-flex justify-content-end my-2">
                                  <div class="btn-group btn-group-sm">
                                   <a data-id="${orders[i].id}" href="#order-detail-${orders[i].id}" data-toggle="collapse" class="btn btn-outline-secondary">查看订单明细</a>
                                   ${orders[i].status==3?'<button type="button" disabled="disabled" class="btn btn-outline-info">已收货</button>':'<button onclick="shouHuo(this)" type="button" class="btn btn-outline-success">确认收货</button>'}
                                   
                                  </div>
                                 </div>
                            
                                 <div id="order-detail-${orders[i].id}" class="collapse collapsed">
                                  <div class="order-detail">
                                   
                                  </div>
                                 </div>
                                </div>`);
                //拼接详情
                var sons=orders[i].sons;
                for (let j = 0; j <sons.length; j++) {
                    var son=sons[j];
                    var det=$(`<div>
                        <span class="text-muted">${son.productname}</span>
                        <span class="text-primary">&nbsp;x${son.productcount}</span>
                        <span class="text-danger">&nbsp;￥${son.productcount*son.productcrice}</span>
                       </div>`);
                    div.find(".order-detail").append(det);
                }
                $("#order").append(div);
            }
        }
    )
}

function shouHuo(obj){
    var id = $(obj).parent().find("a").data("id");
    $.get(
        "OrderUpdataStus",
        {id:id,status:3},
        function (msg){
            console.log(msg.data)
            console.log(msg)
            if(msg.data==1){
                //刷新页面
                location.reload();
            }else{
                alert("订单异常");
            }
        },
        "json"
    )
}
